//
//  NSDate+SNExtension.m
//  Truck
//
//  Created by zhiqichen on 16/3/25.
//  Copyright © 2016年 樊皓天. All rights reserved.
//

#import "NSDate+SNExtension.h"

@implementation NSDate (SNExtension)

/** 根据日期和日期格式返回字符串 */
+ (NSString *)stringWithDate:(NSDate *)date dateFormat:(NSString *)dateFormat
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
    formatter.dateFormat = dateFormat;
    NSString *string = [formatter stringFromDate:date];
    
    return string;
}

/** 根据传入的天数返回天数的NSdate */
- (NSDate *)dateFromFutureDay:(NSInteger)day
{
    NSTimeInterval timeInterval = [self timeIntervalWithDay:day];
    NSDate *futureDate = [NSDate dateWithTimeInterval:timeInterval sinceDate:self];
    return futureDate;
}

/** 根据传入的秒数返回NSDate */
- (NSDate *)dateFromFutureTimeInterval:(NSInteger)timeInterval
{
    NSTimeInterval times = [self timeIntervalWithSecond:timeInterval];
    NSDate *futureDate = [NSDate dateWithTimeInterval:times sinceDate:self];
    
    return futureDate;
}

#pragma mark - 根据传入的天数返回NSTimeInterval
- (NSTimeInterval)timeIntervalWithDay:(NSInteger)day
{
    return [self timeIntervalWithHour:day * 24];
}
- (NSTimeInterval)timeIntervalWithHour:(NSInteger)hour
{
    return [self timeIntervalWithMinutes:hour * 60];
}
- (NSTimeInterval)timeIntervalWithMinutes:(NSInteger)minutes
{
    return [self timeIntervalWithSecond:minutes * 60];
}
- (NSTimeInterval)timeIntervalWithSecond:(NSInteger)second
{
    return second;
}

/** 根据字符串返回NSDate */
+ (NSDate *)dateWithString:(NSString *)string
{
    if (string.length < 10) {
        string = [NSString stringWithFormat:@"%@:00-00-00", string];
    }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd:HH-mm-ss";
    NSDate *date = [dateFormatter dateFromString:string];
    
    return date;
}

#pragma mark - 返回self的年份
- (NSInteger)year
{
    return [self componentsWithNSCalendarUnit:NSCalendarUnitYear].year;
}
#pragma mark - 返回self的月份
- (NSInteger)month
{
    return [self componentsWithNSCalendarUnit:NSCalendarUnitMonth].month;
}
#pragma mark - 返回self的天数
- (NSInteger)day
{
    return [self componentsWithNSCalendarUnit:NSCalendarUnitDay].day;
}

#pragma mark - 返回self的小时
- (NSInteger)hour
{
    return [self componentsWithNSCalendarUnit:NSCalendarUnitHour].hour;
}

#pragma mark - 返回self的分钟
- (NSInteger)minute
{
    return [self componentsWithNSCalendarUnit:NSCalendarUnitMinute].minute;
}

#pragma mark - 根据calendarUnit返回self的NSDateComponents
- (NSDateComponents *)componentsWithNSCalendarUnit:(NSCalendarUnit)calendarUnit
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    return [calendar components:calendarUnit
                       fromDate:self];;
} 

#pragma mark - 根据天数返回将来的日期
- (NSString *)futureDayWithDays:(NSInteger)days
{
    return [self futureDayWithDays:days
                     dateFormatter:@"yyyy-MM-dd:HH-mm-ss"];
}
#pragma mark - 根据天数返回将来的日期
- (NSString *)futureDayWithDays:(NSInteger)days
                  dateFormatter:(NSString *)dateFormatter
{
    NSTimeInterval timeInterval = [self hoursWithDays:days];
    
    NSDate *date     = [NSDate dateWithTimeInterval:timeInterval
                                          sinceDate:self];
    
    NSString *string = [self stringWithDate:date
                              dateFormatter:dateFormatter];
    return string;
}

#pragma mark - 根据date和日期格式返回字符串
- (NSString *)stringWithDate:(NSDate *)date
               dateFormatter:(NSString *)dateformatter
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = dateformatter;
    NSString *dateString     = [dateFormatter stringFromDate:date];
    
    return dateString;
}

#pragma mark - 根据传入的天数返回一共多少秒
- (NSTimeInterval)hoursWithDays:(NSInteger)days
{
    return [self minuteWithHours:days * 24];
}
#pragma mark - 根据传入的小时返回一共多少秒
- (NSTimeInterval)minuteWithHours:(NSTimeInterval)hours
{
    return [self secondWithMinutes:hours * 60];
}
#pragma mark - 根据传入的分钟返回一共多少秒
- (NSTimeInterval)secondWithMinutes:(NSTimeInterval)minutes
{
    return minutes * 60;
}

#pragma mark - 根据日期格式返回字符串
- (NSString *)stringWithDateformatter:(NSString *)dateformatter
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = dateformatter;
    NSString *dateString     = [dateFormatter stringFromDate:self];
    
    return dateString;
}

//判断日期是今天，昨天还是明天
+(NSString *)compareDate:(NSString *)time{
    
    
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *fromdate=[format dateFromString:time];
    
    NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
    NSInteger frominterval = [fromzone secondsFromGMTForDate: fromdate];
    NSDate *date = [fromdate  dateByAddingTimeInterval: frominterval];
    
    NSTimeInterval secondsPerDay = 24*60*60;
    NSDate *today = [[NSDate alloc] init];
    NSDate *tomorrow,*yesterday,*afterDay;
    tomorrow = [today dateByAddingTimeInterval:secondsPerDay];
    afterDay = [today dateByAddingTimeInterval:secondsPerDay*2];
    yesterday = [today dateByAddingTimeInterval:-secondsPerDay];
    NSString *todayString = [[today description]substringToIndex:10];
    NSString *yesterdayString = [[yesterday description]substringToIndex:10];
    NSString *tomorrowString = [[tomorrow description]substringToIndex:10];
    NSString *afterDayString = [[afterDay description]substringToIndex:10];
    NSString *dateString = [[date description]substringToIndex:10];
    if ([dateString isEqualToString:todayString]) {
        return @"今天";
    }else if ([dateString isEqualToString:yesterdayString])
    {
        return @"昨天";
    }
    else if ([dateString isEqualToString:tomorrowString])
    {
        return @"明天";
    }
    else if ([dateString isEqualToString:afterDayString])
    {
        return @"后天";
    }
    else
    {
        return dateString;
    }
}


@end
